﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ChiTietHoaDonNhapList : System.Web.UI.Page
    {
       
        CHUCNANG objchucnang = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            KiemtraQUyenSuDung();
            if (!IsPostBack)
            {
                LoadHienthi();
            }
        }
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHITIETHOADONNHAPRepository repository = new CHITIETHOADONNHAPRepository(new EFRepository<CHITIETHOADONNHAP>(), iunitfwork);
            IQueryable<CHITIETHOADONNHAP> objHoadonN = repository.All();
            GrchitietHdn.DataSource = objHoadonN;
            GrchitietHdn.DataBind();
        }

        protected void GrchitietHdn_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbtnsua")
            {
                DateTime dt = Commomcs.convertDate(DateTime.Now.ToString("dd/MM/yyyy"));
                IUnitOfWork iunitfwork = new EFUnitOfWork();
                CHITIETHOADONNHAPRepository repository = new CHITIETHOADONNHAPRepository(new EFRepository<CHITIETHOADONNHAP>(), iunitfwork);
                string mahdn = e.CommandArgument.ToString();
                CHITIETHOADONNHAP objChitietHDN = repository.All().Where(p => p.MAHDN ==mahdn).FirstOrDefault();
                if (objChitietHDN != null)
                {
                    if (objChitietHDN.HOADONNHAP.NGAYNHAP.Equals(dt))
                    {
                        Response.Redirect("ChiTietHoaDonNhapAdd.aspx?MAHDN=" + e.CommandArgument);
                    }
                    else
                    {
                        RegisterClientScriptBlock("Thông báo", "<script> alert('Chỉ được sửa những hóa đơn nhập trong ngày')</script>");
                        return;
                    }
                }
            }
            if(e.CommandName=="lbtnxoa")
            {
                try
                {
                    IUnitOfWork iunitfwork = new EFUnitOfWork();
                    HOADONNHAPRepository repositorynhap = new HOADONNHAPRepository(new EFRepository<HOADONNHAP>(), iunitfwork);
                    HOADONNHAP objhdnhap = repositorynhap.GetById(e.CommandArgument.ToString());
                    repositorynhap.Delete(objhdnhap);
                    repositorynhap.Save();
                    CHITIETHOADONNHAPRepository repository = new CHITIETHOADONNHAPRepository(new EFRepository<CHITIETHOADONNHAP>(), iunitfwork);
                    CHITIETHOADONNHAP objHoaDonN = repository.GetById(e.CommandArgument.ToString());
                    repository.Delete(objHoaDonN);
                    repository.Save();
                    LoadHienthi();
                }
                catch (Exception ex)
                {
                    RegisterClientScriptBlock("Thông báo", "<script> alert('Không thể xóa được')</script>");
                }
            }
        }
        public void KiemtraQUyenSuDung()
        {
            lbtnthemmoi.Visible = false;
            if (Session["MANV"] != null)
            {
                string strMANV = Session["MANV"].ToString();
                IUnitOfWork iunitwork = new EFUnitOfWork();
                lbtnthemmoi.Visible = false;
                NHANVIENRepository repository = new NHANVIENRepository(new EFRepository<NHANVIEN>(), iunitwork);
                NHANVIEN objNhanvien = repository.All().Where(p => p.MANV == strMANV).FirstOrDefault();
                if (objNhanvien != null)
                {
                    objchucnang = Commomcs.QuyenTruyCapChucnang(Request.Url.AbsolutePath, objNhanvien.MANV);
                    if (objchucnang != null)
                    {
                        if (objchucnang.ISADD == true)
                        {
                            lbtnthemmoi.Visible = true;
                        }
                        else
                        {
                            lbtnthemmoi.Visible = false;
                        }

                    }
                }
            }
        }
        protected void GrchitietHdn_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            Label lbtinhtrang = ((Label)e.Row.FindControl("lbtinhtrang"));
            Label lbmathang = ((Label)e.Row.FindControl("lbmathang"));
            CHITIETHOADONNHAP objCHitTietHDN = ((CHITIETHOADONNHAP)e.Row.DataItem);
            if (objCHitTietHDN != null)
            {
                if (objCHitTietHDN.TINHTRANG == true)
                {
                    lbtinhtrang.Text = "Đã thanh toán";
                }
                else
                {
                    lbtinhtrang.Text = "Chưa thanh toán";
                }
                if (!string.IsNullOrEmpty(objCHitTietHDN.MAMH))
                {
                    lbmathang.Text = objCHitTietHDN.MATHANG.TENMH;
                }
                Label lbdongia = ((Label)e.Row.FindControl("lbdongia"));
                Label lbtongtien = ((Label)e.Row.FindControl("lbtongtien"));
                Label lbghichu = ((Label)e.Row.FindControl("lbghichu"));
                lbdongia.Text = objCHitTietHDN.HOADONNHAP.DONGIA.ToString();
                lbtongtien.Text = objCHitTietHDN.HOADONNHAP.TONGTIEN.ToString();
                lbghichu.Text = objCHitTietHDN.HOADONNHAP.GHICHU;

            }
            LinkButton lbtnsua = ((LinkButton)e.Row.FindControl("lbtnsua"));
            LinkButton lbtnxoa = ((LinkButton)e.Row.FindControl("lbtnxoa"));
            lbtnsua.Visible = false;
            lbtnxoa.Visible = false;
            if (objchucnang != null)
            {
                
                lbtnsua.Visible = false;
                if (lbtnsua != null)
                {
                    if (objchucnang.ISEDIT == true)
                    {
                        lbtnsua.Visible = true;
                    }
                    else
                    {
                        lbtnsua.Visible = false;
                    }
                }
                lbtnxoa.Visible = false;
                if (lbtnxoa != null)
                {
                    if (objchucnang.ISDELETE == true)
                    {
                        lbtnxoa.Visible = true;
                    }
                    else
                    {
                        lbtnxoa.Visible = false;
                    }
                }
            }



        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChiTietHoaDonNhapAdd.aspx");
        }
    }
}